package Dm

import "fmt"

var (
	getNestedTableSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT \"TABLE_NAME\" from %v where owner = '%s' %v", o, s, t)
	}
	getTableNameSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT \"TABLE_NAME\" from %v where owner = '%s' %v", o, s, t)
	}
	getSeqNameSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT \"SEQUENCE_NAME\" from %v where SEQUENCE_OWNER ='%v' %v", o, s, getLikeTable(t))
	}
	getTableExistenceSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT \"TABLE_NAME\" from %v where owner = '%s' and table_NAME = '%s'", o, s, t)
	}
	getSeqExistenceSql = func(o, s, t string) string {
		return fmt.Sprintf("select \"SEQUENCE_NAME\" from %v where SEQUENCE_OWNER = '%v' AND SEQUENCE_NAME='%v'", o, s, t)
	}
	getSchemaNameSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT username  FROM %v WHERE username = '%v'", o, s)
	}
	getTableCommentSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT comments as \"comment\" FROM %v WHERE OWNER = '%v' AND TABLE_NAME='%v'", o, s, t)
	}
	getColumnCommentSql = func(o, s, t string) string {
		return fmt.Sprintf("select COLUMN_NAME as \"columnName\",to_char(comments ) AS \"columnComment\" from %v where owner = '%v' and TABLE_NAME  = '%v' ", o, s, t)
	}
	getTableCharacterSql = func(o, s, t string) string {
		return fmt.Sprintf("")
	}
	getTableCollationSql = func(o, s, t string) string {
		return fmt.Sprintf("")
	}
	getTableConstraintsSql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT constraint_name as \"name\",search_condition as \"body\" FROM %v WHERE OWNER = '%v' AND table_name = '%v' AND constraint_type = 'C'", o, s, t)
	}
	getSeqBodySql = func(o, s, t string) string {
		return fmt.Sprintf("SELECT 'seq' as \"type\",SEQUENCE_OWNER AS \"schema\",SEQUENCE_NAME AS \"name\",MIN_VALUE AS \"minValue\",MAX_VALUE AS \"maxValue\",INCREMENT_BY AS \"incrementBy\",CYCLE_FLAG AS \"cycleFlag\",ORDER_FLAG AS \"orderFlag\",CACHE_SIZE AS \"cacheSize\",LAST_NUMBER AS \"startValue\" FROM %v WHERE SEQUENCE_OWNER='%v' and SEQUENCE_NAME='%v'", o, s, t)
	}
	getLobColumnSql = func(o, s, t string) string {
		return fmt.Sprintf("select column_name as \"columnName\",data_type as \"columnType\" from %v where OWNER = '%s' AND TABLE_NAME = '%s' AND data_type like '%%LOB%%'", o, s, t)
	}
	getBigColumnSql = func(o, s, t string) string {
		return fmt.Sprintf("select column_name as \"columnName\",data_type as \"columnType\" from %v where OWNER = '%s' AND TABLE_NAME = '%s' and DATA_TYPE = 'VARCHAR2' AND DATA_LENGTH >= 1000", o, s, t)
	}
)

var (
	createSeqBodySql = func(s, t, mi, ma, in, st, ca, or, cy string) string {
		return fmt.Sprintf(
			"CREATE SEQUENCE \"%v\".\"%v\" \n"+
				"\t MINVALUE %v \n"+
				"\t MAXVALUE %v \n"+
				"\t INCREMENT BY %v \n"+
				"\t START WITH %v \n "+
				"\t %v %v %v ; \n", s, t, mi, ma, in, st, ca, or, cy)
	}
	dropSeqSql = func(s, t string) string {
		return fmt.Sprintf("DROP SEQUENCE IF EXISTS \"%v\".\"%v\"", s, t)
	}
	dropTableSql = func(s, t string) string {
		return fmt.Sprintf("BEGIN\n        "+
			"EXECUTE IMMEDIATE 'DROP TABLE %v.%v';\n    "+
			"EXCEPTION\n        "+
			"WHEN OTHERS THEN\n            "+
			"IF SQLCODE != -942 THEN\n                "+
			"RAISE;\n            "+
			"END IF;\n    END;", s, t)
	}
	renameTableSql = func(s, t, ss, tt string) string {
		return fmt.Sprintf("ALTER TABLE \"%v\".\"%v\" RENAME TO \"%v\".\"%v\"", s, t, ss, tt)
	}
	createDatabaseSql = func(s string) string {
		return fmt.Sprintf("CREATE USER IF NOT EXISTS `%v`", s)
	}
	likeTableSql = func(s, t, ss, tt string) string {
		return fmt.Sprintf("CREATE TABLE \"%v\".\"%v\" AS SELECT * FROM \"%v\".\"%v\" WHERE 1=0", s, t, ss, t)
	}
	truncateTableSql = func(s, t string) string {
		return fmt.Sprintf("TRUNCATE TABLE \"%v\".\"%v\"", s, t)
	}
	truncatePartSql = func(s, t, p string) string {
		return fmt.Sprintf("ALTER TABLE \"%v\".\"%v\" TRUNCATE PARTITION \"%v\"", s, t, p)
	}
	analyzeTableSql = func(s, t string) string {
		return fmt.Sprintf("BEGIN \n"+
			"DBMS_STATS.GATHER_TABLE_STATS(\n    "+
			"ownname => '%v',\n    "+
			"tabname => '%v',\n    "+
			"estimate_percent => 100, -- 估算的百分比，100 表示收集所有行\n    "+
			"cascade => TRUE           -- 是否同时收集索引的统计信息\n"+
			");\n"+
			"END;\n"+
			"/", s, t)
	}
	analyzeTableColumnSql = func(s, t string) string {
		return fmt.Sprintf("BEGIN\n    "+
			"DBMS_STATS.GATHER_TABLE_STATS(\n        "+
			"OWNNAME => '%v',\n        "+
			"TABNAME => '%v',\n        "+
			"ESTIMATE_PERCENT => 1.0\n        "+
			"METHOD_OPT => 'FOR ALL COLUMNS SIZE AUTO'\n    "+
			");\n"+
			"END;\n"+
			"/", s, t)
	}
)
